package com.whiteape.book.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.ImmersionBar;
import com.hwangjr.rxbus.RxBus;
import com.whiteape.book.R;
import com.whiteape.book.WaApp;
import com.whiteape.book.model.type.BookSortListType;
import com.whiteape.book.model.zssq.BookSubSortBean;
import com.whiteape.book.ui.base.BaseTabActivity;
import com.whiteape.book.ui.fragment.BookSortListFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BookCategoryDetailActivity extends BaseTabActivity {
    private static final String EXTRA_GENDER = "extra_gender";
    private static final String EXTRA_SUB_SORT = "extra_sub_sort";
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private BookSubSortBean mSubSortBean;
    private String mGender;

    public static void startActivity(Context context, String gender, BookSubSortBean subSortBean) {
        Intent intent = new Intent(context, BookCategoryDetailActivity.class);
        intent.putExtra(EXTRA_GENDER, gender);
        intent.putExtra(EXTRA_SUB_SORT, subSortBean);
        context.startActivity(intent);
    }

    @Override
    protected int getContentId() {
        return R.layout.activity_book_sort_list;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .statusBarColor(R.color.colorPrimary).init();
    }

    @Override
    protected void initView() {
        super.initView();
        setUpAdapter();


//        getWindow().getDecorView().setBackgroundColor(WaApp.getInstance().getResources().getColor(R.color.statusBarColor));
        mTlIndicator.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorAccent));

//        mTlIndicator.setTabTextColors(ColorUtil.isColorLight(ThemeStore.primaryColor(this)) ? Color.BLACK : Color.WHITE,
//                ThemeStore.accentColor(this));
        mTlIndicator.setTabTextColors(Color.BLACK, getResources().getColor(R.color.colorAccent));
    }


    @Override
    protected void setUpToolbar(Toolbar toolbar) {
        super.setUpToolbar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setTitle(mSubSortBean.getMajor());
        }

//        if (getSupportActionBar() != null) {
//        }
        toolbar.setNavigationOnClickListener((v) -> finish());
    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (savedInstanceState != null) {
            mGender = savedInstanceState.getString(EXTRA_GENDER);
            mSubSortBean = savedInstanceState.getParcelable(EXTRA_SUB_SORT);
        } else {
            mGender = getIntent().getStringExtra(EXTRA_GENDER);
            mSubSortBean = getIntent().getParcelableExtra(EXTRA_SUB_SORT);
        }
        if (mSubSortBean == null) {
            mGender = "male";
            mSubSortBean = new BookSubSortBean();
            mSubSortBean.setMajor("玄幻");
            mSubSortBean.setMins(Arrays.asList("东方玄幻", "异界大陆", "异界争霸", "远古神话"));
        }

//        Objects.requireNonNull(getSupportActionBar()).setTitle(mSubSortBean.getMajor());

//        setupActionBar();
    }

//    @Override
//    protected void setUpToolbar(Toolbar toolbar) {
//        super.setUpToolbar(toolbar);
//        getSupportActionBar().setTitle(mSubSortBean.getMajor());
//    }

//    @Override
//    protected void initData(Bundle savedInstanceState) {
//        super.initData(savedInstanceState);
//        if (savedInstanceState != null){
//            mGender = savedInstanceState.getString(EXTRA_GENDER);
//            mSubSortBean = savedInstanceState.getParcelable(EXTRA_SUB_SORT);
//        }
//        else {
//            mGender = getIntent().getStringExtra(EXTRA_GENDER);
//            mSubSortBean = getIntent().getParcelableExtra(EXTRA_SUB_SORT);
//        }
//    }

    @Override
    protected List<Fragment> createTabFragments() {
        List<Fragment> fragments = new ArrayList<>();
        for (BookSortListType type : BookSortListType.values()) {
            fragments.add(BookSortListFragment.newInstance(mGender, mSubSortBean.getMajor(), type));
        }
        return fragments;
    }

    @Override
    protected List<String> createTabTitles() {
        List<String> titles = new ArrayList<>();
        for (BookSortListType type : BookSortListType.values()) {
            titles.add(type.getTypeName());
        }
        return titles;
    }

//    @Override
//    protected void initClick() {
//        super.initClick();
//        mTagAdapter.setOnItemClickListener(
//                (view,pos) -> {
//                    String subType = mTagAdapter.getItem(pos);
//                    RxBus.getInstance().post(new BookSubSortEvent(subType));
//                }
//        );
//    }

//    @Override
//    protected void initWidget() {
//        super.initWidget();
//        setUpAdapter();
//    }

    private void setUpAdapter() {
//        mTagAdapter = new HorizonTagAdapter();
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        mRvTag.setLayoutManager(linearLayoutManaer);
//        mRvTag.setAdapter(mTagAdapter);

//        mSubSortBean.getMins().add(0, "全部");
//        mTagAdapter.addItems(mSubSortBean.getMins());
    }

    /*****************************************************/
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(EXTRA_GENDER, mGender);
        outState.putParcelable(EXTRA_SUB_SORT, mSubSortBean);
    }
}
